package org.libspark.betweenas3.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 绘制全图，无需调整注册点
	 * @author jack
	 */
	public class BitmapUtil
	{
		
		/**
		 * 绘制位图
		 * @param source
		 * @param repairRegistPoint
		 * @param inflatePoint
		 * @return 
		 */
		public static function drawBitmap(source:*, repairRegistPoint:Boolean=false,transparent:Boolean=true,fillColor:uint=0,inflatePoint:Point=null,mirroring_x:Boolean=false,mirroring_y:Boolean=false):Bitmap
		{
			var rect:Rectangle=source.getBounds(source.parent);
			if(!rect.isEmpty()){
				if(inflatePoint)rect.inflatePoint(inflatePoint);
				var offsetx:int=source.x - rect.x;
				var offsety:int=source.y - rect.y;
				var _matrix:Matrix=new Matrix(1, 0, 0, 1, offsetx, offsety);
				if(mirroring_x){
					_matrix.tx=rect.width-_matrix.tx;
					_matrix.a=-1
				}
				if(mirroring_y){
					_matrix.ty=rect.height-_matrix.ty;
					_matrix.d=-1
				}
				//_matrix.tx*=-1
				var bmd:BitmapData=new BitmapData(rect.width, rect.height,transparent, fillColor);
				bmd.draw(source, _matrix);
			}else{
				return null;
			}
			var bitmap:Bitmap=new Bitmap(bmd);
			if (repairRegistPoint)
			{
				bitmap.x=offsetx * -1;
				bitmap.y=offsety * -1;
			}
			return bitmap;
		}
		/**
		 * 绘制位图
		 * @param source
		 * @param repairRegistPoint
		 * @param inflatePoint
		 * @return 
		 */
		public static function drawObject(source:*, repairRegistPoint:Boolean=false,transparent:Boolean=true,fillColor:uint=0,inflatePoint:Point=null,mirroring_x:Boolean=false,mirroring_y:Boolean=false):Object
		{
			var rect:Rectangle=source.getBounds(source.parent);
			if(!rect.isEmpty()){
				if(inflatePoint)rect.inflatePoint(inflatePoint);
				var offsetx:int=source.x - rect.x;
				var offsety:int=source.y - rect.y;
				var _matrix:Matrix=new Matrix(1, 0, 0, 1, offsetx, offsety);
				if(mirroring_x){
					_matrix.tx=rect.width;
					_matrix.a=-1
				}
				if(mirroring_y){
					_matrix.ty=rect.height;
					_matrix.d=-1
				}
				var bmd:BitmapData=new BitmapData(rect.width, rect.height,transparent, fillColor);
				bmd.draw(source, _matrix);
			}else{
				return null;
			}
			return {bitmapData:bmd,offsetPoint:new Point(offsetx,offsety)};
		}
		
		/**
		 *对位图进行颜色采样操作
		 * @param source
		 * @param isAllPixel
		 * @param findcolor
		 * @return 
		 * 
		 */	
		public static function drawPixelShapeByBitmapDataColor(source:BitmapData,isAllPixel:Boolean=false,findcolor:uint=0):Shape
		{
			var s:Shape = new Shape();
			var _w:uint=source.width;
			var _h:uint=source.height;
			var i:int;
			var j:int;
			if(isAllPixel){
				var prevColor:uint=0;
				var currentColor:uint=0;
				for(i = 0;i < _w;i++)
				{
					for(j = 0;j < _h;j++)
					{
						currentColor=source.getPixel32(i,j)
						if (currentColor){
							if (currentColor!=prevColor){
								if(!prevColor)s.graphics.endFill();
								s.graphics.beginFill(currentColor);
								prevColor=currentColor;
							}
							s.graphics.drawRect(i,j,1,1);
						}
					}
				}
				s.graphics.endFill();
			}else{
				s.graphics.beginFill(findcolor);
				for(i = 0;i < _w;i++)
				{
					for(j = 0;j < _h;j++)
					{
						if (source.getPixel32(i,j)==findcolor)
							s.graphics.drawRect(i,j,1,1);
					}
				}
				s.graphics.endFill();
			}
			
			return s;
		}
	}
}